<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>添加用户</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="${base}/js/lib/extjs/resources/css/ext-all.css"/>
	<script type="text/javascript" src="${base}/js/lib/extjs/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="${base}/js/lib/extjs/ext-all-debug.js"></script>
	<script type="text/javascript" src="${base}/js/lib/extjs/ext-lang-zh_CN.js"></script>
  </head>
  
  <body>
    <script type="text/javascript">
	function reloadCaptcha() {
		var img = document.getElementById("captchaImg");
		img.src = "${base}/captcha.do?r=" + Math.floor(Math.random() * 100);
	}
	
	Ext.onReady(function(){
		
		var levelStore = new Ext.data.SimpleStore({
			fields:['value','text'],
	    	data:[
	    	      ['1','1级'],
	    	      ['2','2级'],
	    	      ['3','3级'],
	    	      ['4','4级'],
	    	      ['5','5级']
			]
	    });
	    
		var parentStore = new Ext.data.Store({
			baseParams: {
		        param1: 'value1',
		        param2: 'value2'
		    },
			proxy : new Ext.data.HttpProxy({
				method:'get',
				url:'${base}/admin/category/get-by-level.do'
			}),
			autoLoad:true
		});
		
	    var fp = new Ext.form.FormPanel({
	        title:'添加节点',
	        id:'settingAdminForm',
	        standardSubmit: true,
	        width:420,
	        renderTo:document.body,
	        frame:true,
	        defaults:{
	            xtype:'textfield',
	            labelAlign:'left',
	            labelWidth:80,
	            width:250
	        },
	        items:[
	            {
	                fieldLabel:'节点名称',
	                isFormField:true,
	                xtype:'textfield',
	                name:'username',
	                id:'username',
	                allowBlank: false,
                    blankText:"请输入节点名称",
                    msgTarget: 'under'
	            },
	            {
	                fieldLabel:'描述信息',
	                isFormField:true,
	                xtype:'textarea',
	                name:'description',
	                id:'description',
	                allowBlank: false,
                    blankText:"请输入描述信息",
                    msgTarget: 'under'
	            },{
	            	fieldLabel:'节点级别',
	            	isFormField:true,
	            	xtype:'combo',
	            	name: "level",
	            	id: "_level",
	            	hiddenName:"level",
	            	store:levelStore,
	            	displayField:'text',
	            	valueField:'value',
	            	editable:false,
	            	triggerAction:"all",
	            	mode:'local',
	                allowBlank: false,
                    blankText:"请选择一个节点级别",
                    msgTarget: 'under',
					listeners:{   
			            select:function(combo, record,index){
			                 try{
			                     var parent=Ext.getCmp('_parent');
			                     parent.clearValue();
			                     parent.store.load();
			                 }
			                 catch(ex){
			                     Ext.MessageBox.alert("错误","数据加载失败。\n"+ex);
			                 }
			            }
        			} 
	            },{
	            	fieldLabel:'父节点',
	            	isFormField:true,
	            	xtype:'combo',
	            	name: "parent",
	            	id: "_parent",
	            	hiddenName:"parent",
	            	store:parentStore,
	            	displayField:'name',
	            	valueField:'id',
	            	editable:false,
	            	triggerAction:"all",
	            	mode:'local',
	                allowBlank: false,
                    blankText:"请选择一个父节点",
                    msgTarget: 'under'
	            },{
	                fieldLabel:'顺序',
	                isFormField:true,
	                xtype:'numberfield',
	                name:'sortValue',
	                id:'sortValue',
	                allowBlank: false,
                    blankText:"请输入顺序",
                    msgTarget: 'under'
	            },{
	                fieldLabel:'验证码',
	                isFormField:true,
	                xtype:'textfield',
	                name:'captcha',
	                id:'captcha',
	                allowBlank: false,
                    blankText:"请输入下面的验证码",
                    msgTarget: 'under'
	            },{
	                fieldLabel:'验证图',
					xtype: 'box',
					html: '<a href="javascript:reloadCaptcha();"><img id="captchaImg" src="${base}/captcha.do" /></a>'
				}
	        ],
	        buttonAlign:'center',
			buttons : [ {
				text : '提交',
				type : 'submit',
				handler : function() {
					if (fp.form.isValid()) {
						Ext.MessageBox.show({
							title : '请稍等',
							msg : '正在加载...',
							progressText : '',
							width : 300,
							progress : true,
							closable : false,
							animEl : 'loding'
						});
						var f = function(v) {
							return function() {
								var i = v / 11;
								Ext.MessageBox.updateProgress(i, '');
							};
						};
						for ( var i = 1; i < 13; i++) {
							setTimeout(f(i), i * 150);
						}
						fp.form.doAction('submit', {
							url : '${base}/admin/user/set-role.do',
							method : 'post',
							params : '',
							success : function(form, action){//Ext.util.JSON.decode(response.responseText);
								if (action.result.msg=='ok') {   
									Ext.Msg.alert('提示','操作已成功。如果您打开了用户列表，请刷新列表，以便能看到更新后的结果。');
									fp.form.reset();   
								}else {   
	                                Ext.Msg.alert('错误',action.result.msg);
								}
								reloadCaptcha();
							},
							failure : function() {
								Ext.Msg.alert('错误', '服务器出现错误请稍后再试！');
								reloadCaptcha();
							}
						});
					}
				}
			}, {
				text : '取消',
				handler : function() {

				}
			} ]
	    });
	}); 
    </script>
  </body>
</html>